/**
 * 
 */
package org.t2.backstage.module.recharge;

import java.util.ArrayList;
import java.util.List;

import org.banish.sql.core.util.QuerySet;
import org.gaming.backstage.PageData;
import org.gaming.ruler.spring.Spring;
import org.gaming.service.apidoc.annotation.ApiDocument;
import org.gaming.tool.ConsoleUtil;
import org.gaming.tool.DateTimeUtil;
import org.gaming.tool.GsonUtil;
import org.gaming.tool.LocalDateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.t2.backstage.module.gameserver.model.GameChannel;
import org.t2.backstage.module.gameserver.model.GameServer;
import org.t2.backstage.module.gameserver.service.GameChannelService;
import org.t2.backstage.module.gameserver.service.GameServerService;
import org.t2.backstage.module.recharge.model.RechargeCallback;
import org.t2.backstage.module.recharge.model.RechargeProductCfg;
import org.t2.backstage.module.recharge.service.RechargeCallbackService;
import org.t2.backstage.module.recharge.service.RechargeProductCfgService;
import org.t2.backstage.module.recharge.struct.RechargeQuery;
import org.t2.backstage.module.recharge.vo.RechargeCallbackVo;

import jakarta.annotation.PostConstruct;

/**
 * @author YY
 *
 */
@Controller
public class RechargeCallbackController {
	
	@Autowired
	private RechargeCallbackService rechargeCallbackService;
	@Autowired
	private GameChannelService gameChannelService;
	@Autowired
	private GameServerService gameServerService;
	@Autowired
	private RechargeProductCfgService rechargeProductCfgService;
	
	
	@ApiDocument("请求充值订单数据")
	@RequestMapping(value = "/recharge/data.auth")
	@ResponseBody
	public PageData<RechargeCallbackVo> data(RechargeQuery query) {
		QuerySet querySet = new QuerySet();
		//渠道选择
		if(query.getChannels() != null && !query.getChannels().isEmpty()) {
			querySet.findInSet("channel_id", query.getChannels());
		}
		//服务器选择
		if(query.getServerUids() != null && !query.getServerUids().isEmpty()) {
			querySet.findInSet("server_uid", query.getServerUids());
		}
		if(query.getPlayerId() > 0) {
			querySet.addCondition("player_id = ?", query.getPlayerId());
		}
		if(query.getNick() != null && !"".equals(query.getNick())) {
			querySet.addCondition("nick like ?", "%" + query.getNick() + "%");
		}
		if(query.getSdkOrderId() != null && !"".equals(query.getSdkOrderId())) {
			querySet.addCondition("sdk_order_id like ?", "%" + query.getSdkOrderId() + "%");
		}
		long startTime = query.getStartTime() * 1000L;
		if(startTime > 0) {
			String startStr = DateTimeUtil.formatMillis(DateTimeUtil.YYYYMMDD, startTime);
			querySet.addCondition("insert_time >= ?", startStr);
		}
		long endTime = query.getEndTime() * 1000L;
		if(endTime > 0) {
			endTime += DateTimeUtil.ONE_DAY_MILLIS;
			String endStr = DateTimeUtil.formatMillis(DateTimeUtil.YYYYMMDD, endTime);
			querySet.addCondition("insert_time <= ?", endStr);
		}
		if(query.getStatus() > 0) {
			querySet.addCondition("give = ?", query.getStatus());
		}
		
		querySet.orderBy("order by id desc");
		querySet.limit(query.getPage(), query.getLimit());
		querySet.formWhere();
		
		List<RechargeCallback> callbacks = rechargeCallbackService.queryCallbacks(querySet);
		
		List<RechargeCallbackVo> resultList = new ArrayList<>();
		for(RechargeCallback callback : callbacks) {
			GameChannel gameChannel = gameChannelService.getEntity(0, callback.getChannelId());
			GameServer gameServer = gameServerService.getByPlatformAndServer(callback.getPlatformId(), callback.getServerId());
			
			RechargeCallbackVo resultVo = new RechargeCallbackVo();
			resultVo.setId(callback.getId());
			resultVo.setRechargeTime(LocalDateTimeUtil.toEpochMilli(callback.getInsertTime()));
			if(gameChannel == null) {
				resultVo.setChannelInfo("");
			} else {
				resultVo.setChannelInfo(gameChannel.getName() + "[" + gameChannel.getId() + "]");
			}
			if(gameServer == null) {
				resultVo.setServerInfo("");
			} else {
				resultVo.setServerInfo(gameServer.getName() + "[" + gameServer.getServerId() + "]");
			}
			resultVo.setSdkOrderId(callback.getSdkOrderId());
			resultVo.setGameOrderId(callback.getGameOrderId());
			resultVo.setPlayerId(callback.getPlayerId());
			resultVo.setNick(callback.getNick());
			resultVo.setPayMoney(callback.getMoney() / 100);
			resultVo.setProductName(callback.getProductName());
			RechargeProductCfg rechargeCfg = rechargeProductCfgService.getEntity(0, callback.getProductId());
			if(rechargeCfg != null) {
				resultVo.setProductType(rechargeCfg.getType());
				resultVo.setRewards(rechargeCfg.getObtainNum());
				resultVo.setPrePayMoney(rechargeCfg.getPrice());
			}
			resultVo.setLevel(callback.getLevel());
			resultVo.setVipLevel(callback.getVipLevel());
			resultVo.setStatus(callback.getGive());
			resultVo.setStatusText(callback.getRemark());
			resultVo.setMemo(callback.getMemo());
			resultList.add(resultVo);
		}
		
		PageData<RechargeCallbackVo> pageData = new PageData<>();
		pageData.setData(resultList);
		pageData.setCount(resultList.size());
		return pageData;
	}
	
	@ApiDocument("人工触发订单补发发货")
	@RequestMapping(value = "/recharge/manual.authw")
	@ResponseBody
	public void manual(@RequestParam("rechargeId") long rechargeId, @RequestParam("memo") String memo) {
		RechargeCallback rechargeCallback = rechargeCallbackService.getEntity(rechargeId);
		rechargeCallback.setMemo(memo);
		rechargeCallbackService.update(rechargeCallback);
		
		rechargeCallbackService.handleCallback(rechargeCallback);
	}
	
	@PostConstruct
	private void console() {
		ConsoleUtil.addFunction("recfg", () -> {
			test();
		});
	}
	
	public void test() {
		RechargeQuery query = new RechargeQuery();
		PageData<RechargeCallbackVo> pageData = this.data(query);
		System.out.println("pageData.getData().size() " + pageData.getData().size());
		for(RechargeCallbackVo vo : pageData.getData()) {
			System.out.println(GsonUtil.toJson(vo));
		}
		
		Spring.getBean(RechargeProductCfgController.class).refresh();
	}
}
